import Vuex from "vuex"
import Vue from "vue"

Vue.use(Vuex)

const store = new Vuex.Store({
    state:{
        cart:[],
        fullprice:0
    },
    mutations:{
        pushCart(state,good){
            for(var item of state.cart){
                if(good.iid===item.iid){
                    item.count++
                    return
                }
            }
            state.cart.push(good);
        },
        jiacount(state,iid){
            for(var item of state.cart){
                if(item.iid===iid){
                    item.count++
                }
            }
        },
        jiancount(state,iid){
            for(var item of state.cart){
                if(item.iid===iid){
                    item.count--
                }
            }
        },
        check(state,iid){
            for(var item of state.cart){
                if(item.iid===iid){
                    item.check=!item.check
                }
            }
        }

    },
    actions:{

    },
    getters:{
        getCart(state){
            return state.cart
        },
        getfullprice(state){
            let sum=0;
            for(var item of state.cart){
                if(item.check){
                sum+=item.count*item.price
                }
            }
            return sum.toFixed(2)
        },
        getcount:(state)=>{
            return iid=>{
                for(var item of state.cart){
                    if(iid===item.iid){
                        return item.count
                    }
                }
            }
        },
        getjisuan(state){
            let sum=0
            for(var item of state.cart){
                if(item.check){
                    sum++
                }
            }
            return sum
        }
    }
})

export default store
